import wx
import wx.lib.agw.gradientbutton as GB



class Book_image(wx.Panel):
    def __init__(self, parent, file_name, image_type, **k):
        wx.Panel.__init__(self, parent, **k)
        self.mimage = wx.Image(file_name, type=image_type)
        mypan = wx.Panel(self)
        self.myBitmap = wx.StaticBitmap(mypan)
        self.myBitmap.SetBitmap(wx.Bitmap(self.mimage))
        msizer = wx.BoxSizer( wx.VERTICAL )
        sizer = wx.BoxSizer( wx.HORIZONTAL )
        add_button = GB.GradientButton(self, -1, None, "+")
        add_button.SetBaseColours(wx.BLUE)
        del_button = GB.GradientButton(self, -1, None, "-")
        del_button.SetBaseColours(wx.BLUE)
        sizer.Add( add_button, 1,  wx.ALL |wx.EXPAND, 5 )
        sizer.Add( del_button, 1,  wx.ALL |wx.EXPAND, 5 )
        msizer.Add( mypan, 1,  wx.EXPAND, 5 )
        msizer.Add( sizer, 0,  wx.ALL | wx.EXPAND, 5 )
        self.SetSizer(msizer)
        add_button.Bind(wx.EVT_BUTTON, lambda eve, value="add" : self.on_button(eve, value)) 
        del_button.Bind(wx.EVT_BUTTON, lambda eve, value="del" : self.on_button(eve, value)) 
        x = self.mimage.GetSize()[0]
        y = self.mimage.GetSize()[1]
        if x > y:
            self.multiple = x/y
        else:
            self.multiple = y/x
        self.Bind(wx.EVT_MOUSEWHEEL, self.on_wheel)
        self.Bind(wx.EVT_CLOSE, self.on_close)

    def on_close(self, eve):
        self.Destroy()

    def on_wheel(self, eve):
        if eve.GetWheelRotation() > 0:
            self.on_button(eve, "add")
        else:
            self.on_button(eve, "del")

    def on_button(self, eve, value):
        msize = self.myBitmap.GetBitmap().GetSize()
        if msize[0] < 100:
            value = "add"
        elif msize[0] > self.mimage.GetSize()[0]:
            value = "del"

        if value == "add":
            img = self.mimage.Scale(msize[0]+50, (msize[0]+50)/self.multiple)
        else:
            img = self.mimage.Scale(msize[0]-50, (msize[0]-50)/self.multiple)
        self.myBitmap.SetBitmap(wx.Bitmap(img))





def main():
    app = wx.App()
    ex = wx.Frame(None)
    Book_image(ex, "jpg.jpg",wx.BITMAP_TYPE_JPEG)
    ex.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()